package com.xxxx.server.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xxxx.server.pojo.*;
import com.xxxx.server.service.IAdminRoleService;
import com.xxxx.server.service.ICollegeService;
import com.xxxx.server.service.IRoleService;
import com.xxxx.server.service.ITclassService;
import com.xxxx.server.service.impl.RoleServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.sql.Wrapper;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 班级表 前端控制器
 * </p>
 *
 * @author zhoubin
 * @since 2022-04-23
 */
@RestController
@RequestMapping("/col/class")
public class TclassController {

    @Autowired
    private ITclassService tclassService;

    @Autowired
    private ICollegeService collegeService;

    @ApiOperation(value = "获取所有学院信息")
    @GetMapping("/getcollege")
    public List<College> getAllCollege(){
        return collegeService.list();
    }



    @ApiOperation(value = "获取所有班级信息")
    @GetMapping("/getclass")
    public List<Tclass> getAllTclass(){
        return tclassService.list();
    }


    @ApiOperation(value = "添加班级信息")
    @PostMapping("/")
    public RespBean addTclass(@RequestBody Tclass tclass){
        if (tclassService.save(tclass)){
            return RespBean.success("添加成功！");
        }
        return RespBean.error("添加失败!");
    }

    @ApiOperation(value = "更新班级信息")
    @PutMapping("/")
    public RespBean updateTclass(@RequestBody Tclass tclass){
        if (tclassService.updateById(tclass)){
            return RespBean.success("更新成功！");
        }
        return RespBean.error("更新失败！");
    }

    @ApiOperation(value = "删除班级信息")
    @DeleteMapping("/{id}")
    public RespBean deleteTclass(@PathVariable Integer id){
        if (tclassService.removeById(id)){
            return RespBean.success("删除成功!");
        }
        return RespBean.error("删除失败！");
    }

    @ApiOperation(value = "批量删除班级信息")
    @DeleteMapping("/")
    public RespBean deleteTclassByIds(Integer[] ids){
        if (tclassService.removeByIds(Arrays.asList(ids))){
            return RespBean.success("删除成功!");
        }
        return RespBean.error("删除失败！");
    }


    //根据角色获取班级信息，学校管理员可以获取所有班级，学院管理员可以获取所管理学院的班级信息。
    @ApiOperation(value = "查询所有的班级（分页）")
    @GetMapping("/getPageTclass/")
    public RespPageBean getTclassPage(@RequestParam(defaultValue = "1") Integer currentPage ,
                                    @RequestParam(defaultValue = "10")Integer size,
                                    Tclass tclass){
        Admin admin = (Admin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        int roleid = admin.getRoles().get(0).getId();
        int collegeid = 0;
        if(roleid!=1){
            collegeid = admin.getCollegeid();
        }
        return tclassService.getTclassByPage(currentPage,size,tclass,roleid,collegeid);
    }


}
